package managedbean.SCM.DeliveryOrder;

import entity.SCM.DeliveryOrder;
import entity.SCM.PickingOrder.State;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.SCM.DeliveryOrderBeanLocal;

@ManagedBean(name = "viewDeliveryOrderManagerBean")
@ViewScoped
public class ViewDeliveryOrderManagerBean implements Serializable{

    public ViewDeliveryOrderManagerBean() {
    }
    
    @EJB
    private DeliveryOrderBeanLocal deliveryOrderBeanLocal;
    private DeliveryOrder selectedDeliveryOrder;
    private String statusMessage;

    public DeliveryOrder getSelectedDeliveryOrder() {
        return selectedDeliveryOrder;
    }

    public void setSelectedDeliveryOrder(DeliveryOrder selectedDeliveryOrder) {
        this.selectedDeliveryOrder = selectedDeliveryOrder;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    
    public List<DeliveryOrder> getDeliveryOrder(){
        return deliveryOrderBeanLocal.getDeliveryOrder();
    }
    
    public void createPickingOrder(ActionEvent event) throws IOException{
        DeliveryOrder selectedDeliveryOrder = this.getSelectedDeliveryOrder();       
        if (selectedDeliveryOrder != null)
        {
            if (selectedDeliveryOrder.getPickingOrder()==null)
            {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedDeliveryOrder", selectedDeliveryOrder);
                FacesContext.getCurrentInstance().getExternalContext().redirect("../pickingOrder/addPickingOrder.xhtml");                
            }
        }
        
    }
    
    public void createPackingOrder(ActionEvent event) throws IOException{
        DeliveryOrder selectedDeliveryOrder = this.getSelectedDeliveryOrder();       
        //this is for testing only, must delete this line before release
        deliveryOrderBeanLocal.updatePickingStatus(selectedDeliveryOrder.getPickingOrder().getId(), entity.SCM.PickingOrder.State.Success);
        
        if (selectedDeliveryOrder.getPickingOrder()!=null)
        {
            if (deliveryOrderBeanLocal.isPickingOrder_Success(selectedDeliveryOrder.getPickingOrder().getId()))
            {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedPickingOrder", selectedDeliveryOrder.getPickingOrder());
                FacesContext.getCurrentInstance().getExternalContext().redirect("../packingOrder/addPackingOrder.xhtml");                
            }
        }
    }
    
    public void createShippingOrder(ActionEvent event) throws IOException{
        DeliveryOrder selectedDeliveryOrder = this.getSelectedDeliveryOrder();      
        
        //this is for testing only, must delete this line before release
        deliveryOrderBeanLocal.updatePackingStatus(selectedDeliveryOrder.getPackingOrder().getId(), entity.SCM.PackingOrder.State.Success);
        
        if (selectedDeliveryOrder.getPackingOrder()!=null)
        {
            if (deliveryOrderBeanLocal.isPackingOrder_Success(selectedDeliveryOrder.getPackingOrder().getId()))
            {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedPackingOrder", selectedDeliveryOrder.getPackingOrder());
                FacesContext.getCurrentInstance().getExternalContext().redirect("../shippingOrder/addShippingOrder.xhtml");                
            }
        }  
    }
    
    public void deleteDeliveryOrder(ActionEvent event) throws IOException{
        
    }
    
    public void test(ActionEvent event) throws IOException
    {
        deliveryOrderBeanLocal.updateDeliveryStatus(selectedDeliveryOrder.getId(), DeliveryOrder.State.Success);        
    }
    
    
}
